﻿using RabbitMQ.Client;
using System;
using System.Text;

namespace RabbitMQSend
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "192.168.0.201" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {

                    string message = string.Empty;
                    byte[] body = null;

                    // channel.QueueDeclare
                    // durable：在服务器重启时，能够存活
                    // exclusive ：是否为当前连接的专用队列，在连接断开后，会自动删除该队列，生产环境中应该很少用到吧
                    // autodelete：当没有任何消费者使用时，自动删除该队列

                    #region 基础
                    channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);

                    message = "Hello World!";
                    body = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
                    Console.WriteLine(" [x] Sent {0}", message);
                    #endregion


                    #region 工作队列模式(Work Queue)
                    channel.QueueDeclare(queue: "task_queue", durable: true, exclusive: false, autoDelete: false, arguments: null);

                    message = "工作队列模式(Work Queue)";
                    body = Encoding.UTF8.GetBytes(message);

                    var properties = channel.CreateBasicProperties();
                    properties.Persistent = true;

                    channel.BasicPublish(exchange: "", routingKey: "task_queue", basicProperties: properties, body: body);
                    Console.WriteLine(" [x] Sent {0}", message);
                    #endregion

                    // channel.exchangeDeclare()
                    // type：有direct、fanout、topic三种
                    // durable：服务器重启会保留下来Exchange。警告：仅设置此选项，不代表消息持久化。即不保证重启后消息还在
                    // autoDelete:true、false.true:当已经没有消费者时，服务器是否可以删除该Exchange


                    #region 发布/订阅模式(Publish/Subscribe)
                    channel.ExchangeDeclare(exchange: "logs", type: "fanout");

                    message = "发布/订阅模式(Publish/Subscribe)";
                    body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "logs", routingKey: "", basicProperties: null, body: body);
                    Console.WriteLine(" [x] Sent {0}", message);
                    #endregion


                    #region 路由模式(Routing)
                    channel.ExchangeDeclare(exchange: "direct_logs", type: "direct");

                    message = "路由模式(Routing)";
                    body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "direct_logs", routingKey: "info", basicProperties: null, body: body);
                    channel.BasicPublish(exchange: "direct_logs", routingKey: "warn", basicProperties: null, body: body);
                    Console.WriteLine(" [x] Sent '{0}':'{1}'", "info", message);
                    Console.WriteLine(" [x] Sent '{0}':'{1}'", "warn", message); 
                    #endregion



                }
            }

            Console.ReadKey();
        }
    }
}
